package com.pdstudio.carrecom.ui.activity.account;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.PhoneValideCode;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 找回密码
 * 类: ActivityGetPassword <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-18 上午11:20:14
 */
public class ActivityGetPassword extends Activity {
	
	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	private EditText mPhone,mValidecode;
	private Button mGetValidecode,mSubmit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account_getpassword);

		initTitle();
		InitialView();
	}
	
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("忘记密码");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
	}
	
	private void InitialView(){
		mPhone=(EditText)findViewById(R.id.et_phonenum);
		mValidecode=(EditText)findViewById(R.id.et_validecode);
		mGetValidecode=(Button)findViewById(R.id.btn_getvalidecode);
		mGetValidecode.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if(mPhone.getText().toString().equals("")||mPhone.getText()==null){
						UIHelper.ToastMessage(ActivityGetPassword.this, "手机号不能为空！");
					}else{
						HttpExecuteJson http = new HttpExecuteJson(ActivityGetPassword.this, mLoginListener);
						RequestParams rps=new RequestParams();
						http.post(ServiceHelper.RegisterCode+"/"+mPhone.getText().toString(), rps);
					}
				} catch (Exception e) {
					e.printStackTrace();
					UIHelper.ToastMessage(ActivityGetPassword.this, "获取验证码出现错误："+e.getMessage());
				}
				
				
			}});
		mSubmit=(Button)findViewById(R.id.btn_submit);
		mSubmit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if(mPhone.getText().toString().equals("")||mPhone.getText()==null){
						UIHelper.ToastMessage(ActivityGetPassword.this, "手机号不能为空！");
					}else{
						HttpExecuteJson http = new HttpExecuteJson(ActivityGetPassword.this, mPassPostListener);
						RequestParams rps=new RequestParams();
						rps.addBodyParameter("phone", mPhone.getText().toString());
						http.post(ServiceHelper.ForgetPassPost, rps);
					}
				} catch (Exception e) {
					e.printStackTrace();
					UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码出现错误："+e.getMessage());
				}
			}});
	}
	
	private void DoJson(String json){
		PhoneValideCode edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<PhoneValideCode>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					mValidecode.setText(edv.code);

				}catch(Exception e){
					e.printStackTrace();
				}
			}else{
				UIHelper.ToastMessage(ActivityGetPassword.this, "获取验证码失败");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private httpReturnJson mLoginListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityGetPassword.this, "获取验证码失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityGetPassword.this, "获取验证码失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

	private void DoJsonPassPost(String json){
		PhoneValideCode edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<PhoneValideCode>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码成功！");
					finish();
				}catch(Exception e){
					e.printStackTrace();
					UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码失败");
				}
			}else{
				UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码失败");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码失败");
		}

	}
	
	private httpReturnJson mPassPostListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityGetPassword.this, "获取密码失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJsonPassPost(result);
		}
	};

	
}
